const db = require('../../db');

function deepCopy(p, c) {　　　　
    var c = c || {};　　　　
    for (var i in p) {　　　　　　
        if (typeof p[i] === 'object') {　　　　　　　　
            c[i] = (p[i].constructor === Array) ? [] : {};　　　　　　　　
            deepCopy(p[i], c[i]);　　　　　　
        } else {　　　　　　　　　
            c[i] = p[i];　　　　　　
        }　　　　
    }　　　　
    return c;　　
}

let addWeekReport = {
    config: {
        description: '周报添加',
        cors: {
            origin: ['*'],
            additionalHeaders: ['cache-control', 'x-requested-with']
        }
    },
    method: 'post',
    path: '/report/add',
    handler: async (request, h) => {

        let request_body = request.payload

        let stateCode;
        let stateMessage;

        let year = request_body.year
        let week = request_body.week
        let reportData = request_body.reportData

        let result = {
            data: undefined,
            stateCode: '',
            stateMessage: ''
        }
        let promise = function () {
            return new Promise((resolve, reject) => {
                let newData;
                console.log('request_body', request_body)
                db.report.findOne({
                    year: year,
                    week: week
                }, (err, docs) => {
                    if (err) {
                        reject(err)
                    }
                    // 无数据
                    if (!docs) {
                        let doc = {
                            year: year,
                            week: week,
                            content: {
                                title: "团队综合工作周报",
                                department: "",
                                reporter: "",
                                children: [{
                                        title: "本周工作汇报",
                                        children: []
                                    },
                                    {
                                        title: "下周工作计划",
                                        children: []
                                    },
                                    {
                                        title: "问题事项（困难点、问题点、风险点）",
                                        children: []
                                    },
                                    {
                                        title: "给部门的建议",
                                        children: []
                                    }
                                ]
                            }
                        }
                        doc.content.children.forEach((item, index) => {
                            if (item.title === request_body.reportData.title) {
                                doc.content.children[index].children.push(request_body.reportData.children)
                            }
                        })
                        db.report.insert(doc)
                        db.report.findOne({
                            year: year,
                            week: week
                        }, (err, docs) => {
                            resolve(docs)
                        })
                        // 有数据
                    } else {
                        let srcData
                        docs.content.children.forEach((item, index) => {
                            if (item.title === request_body.reportData.title) {
                                srcData = deepCopy(docs)
                                delete srcData._id
                                docs.content.children[index].children.push(request_body.reportData.children)
                            }
                        })
                        db.report.update(srcData, {
                            $set: docs
                        })
                        db.report.findOne({
                            year: year,
                            week: week
                        }, (err, docs) => {
                            resolve(docs)
                        })
                    }
                })
            })
        }
        return await promise()
    }
}

module.exports = addWeekReport